//
//  BasePresentedViewController.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/31.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

/// Abstract class
class BasePresentedViewController: UIViewController {

    //MARK: - Constants
    struct Metric {
        static let bottomButtonMargin: CGFloat = 25
        static let bottomButtonSize = CGSize(width: 240, height: 50)
    }

    func present(by controller: UIViewController) {
        modalPresentationStyle = .overCurrentContext
        modalTransitionStyle = .crossDissolve
        controller.present(self, animated: true, completion: nil)
    }

    enum SizeStyle {
        case small
        case medium
        case large
        var size: CGSize {
            switch self {
            case .small: return CGSize(width: 600, height: 450)
            case .medium: return CGSize(width: 900, height: 560)
            case .large: return CGSize(width: 900, height: 635)
            }
        }
    }

    var sizeStyle: SizeStyle = .medium {
        didSet {
            containerView.mas_remakeConstraints { (make) in
                make?.center.equalTo()(view)
                make?.size.equalTo()(sizeStyle.size)
            }
        }
    }

    enum ConfirmButtonStyle {
        case none
        case innerBottom
        case outerBottom
        case lowerRight

        var isHidden: Bool {
            switch self {
            case .none: return true
            default: return false
            }
        }
    }

    let containerView: UIView = {
        let containerView = UIView()
        containerView.layer.cornerRadius = 8
        containerView.backgroundColor = .white
        return containerView
    }()

    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleToFill
        return imageView
    }()

    lazy var contentView: UIView = {
        let view = UIView()
        self.containerView.addSubview(view)
        view.mas_makeConstraints { (make) in
            make?.edges.equalTo()(self.containerView)?.insets()(UIEdgeInsetsMake(120, 78, 130, 60))
        }
        return view
    }()

    lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(confirm), for: .touchUpInside)
        return button
    }()

    var confirmButtonStyle: ConfirmButtonStyle = .none {
        didSet {
            confirmButton.isHidden = confirmButtonStyle.isHidden
            if case .none = confirmButtonStyle {
                return
            }
            configConfirmButton()
        }
    }

    var bgImage: UIImage? {
        set {
            bgImageView.image = newValue?.stretch(withInsetsTop: 150, left: 150, bottom: 150, right: 150)
            addBGImage()
        }
        get {
            return bgImageView.image
        }
    }

    var showBGImageView = false {
        didSet {
            bgImageView.isHidden = !showBGImageView
            addBGImage()
        }
    }

    @objc func confirm() {
        dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0, alpha: 0.3)
        view.addSubview(containerView)

        sizeStyle = .medium
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }

        let location =  touch.location(in: view)
        if containerView.frame.contains(location) || (confirmButtonStyle != .none && confirmButton.frame.contains(location)) {
            super.touchesEnded(touches, with: event)
        } else {
            dismiss(animated: true, completion: nil)
        }
    }

}

//MARK: - Private method
private extension BasePresentedViewController {

    private func addBGImage() {
        if bgImageView.superview == nil {
            containerView.addSubview(bgImageView)
            bgImageView.mas_makeConstraints { (make) in
                make?.edges.equalTo()(containerView)?.insets()((-10).insets)
            }
        }

    }

    private func configConfirmButton() {
        switch confirmButtonStyle {
        case .innerBottom, .outerBottom:
            confirmButton.layer.cornerRadius = 8
            confirmButton.layer.masksToBounds = true
            confirmButton.setBackgroundImage(UIImage(color: .red), for: .normal)
            confirmButton.setTitleColor(.white, for: .normal)
            confirmButton.titleLabel?.font = .systemFont(ofSize: 14)

        case .lowerRight:
            confirmButton.setBackgroundImage(#imageLiteral(resourceName: "custom_embroidery_btn").alwaysOriginal(), for: .normal)
            confirmButton.setTitleColor(.white, for: .normal)
            confirmButton.titleLabel?.font = UIFont.systemMediumFont(withSize: 16)
        case .none: break
        }

        if confirmButton.superview == nil {
            containerView.addSubview(confirmButton)

            confirmButton.mas_makeConstraints { (make) in
                switch confirmButtonStyle {
                case .outerBottom:
                    make?.top.equalTo()(containerView.mas_bottom)?.offset()(Metric.bottomButtonMargin)
                    make?.centerX.equalTo()(containerView)
                    make?.size.equalTo()(Metric.bottomButtonSize)

                case .innerBottom:
                    make?.size.equalTo()(Metric.bottomButtonSize)
                    make?.centerX.equalTo()(containerView)
                    make?.bottom.equalTo()(containerView)?.offset()(-Metric.bottomButtonMargin)

                case .lowerRight:
                    make?.right.equalTo()(containerView)?.offset()(-50)
                    make?.bottom.equalTo()(containerView)?.offset()(-20)

                case .none: break
                }
            }
        }
    }
}
